package com.djdg.foodies.admin.base.config;

import com.djdg.foodies.admin.base.interceptor.SecuredInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.Resource;

/**
 * @Description:
 * @Auther Demon
 * @Date 2017/12/15 18:58 星期五
 */
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Resource
    private SecuredInterceptor securedInterceptor;

    /**
     * 处理跨域问题
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                //允许的方法
                .allowedMethods("GET", "POST", "OPTIONS", "PUT","DELETE")
                //允许的请求头
                .allowedHeaders("Content-Type",
                        "X-Requested-With",
                        "accept",
                        "Origin",
                        "Access-Control-Request-Method",
                        "Access-Control-Request-Headers",
                        "token",
                        "Access-Control-Allow-Origin",
                        "openId",
                        "null",
                        "region",
                        "client",
                        "User-Agent")
                .exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials")
                .allowCredentials(true).maxAge(3600);
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //注册 登录权限校验拦截器
        registry.addInterceptor(securedInterceptor).addPathPatterns("/**");


    }

}
